package net.siufung.market.base.tenant.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.tenant.mapper.ITenantTypeMapper;
import net.siufung.market.base.tenant.mapper.entity.TenantType;

/**
 * @author 陈建峰
 * @since 2022/6/24 3:41 下午
 */
@AllArgsConstructor
public class TenantTypeCreateRegulation extends AbstractRegulation<TenantType> {

    private static final long serialVersionUID = 8061079233391323848L;

    private final ITenantTypeMapper tenantTypeMapper;

    @Override
    public boolean isSatisfiedBy(TenantType tenantType) {
        VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
        verifyRepeatDto.setMethodEventEnum(MethodEventEnum.ADD);
        verifyRepeatDto.setPk(null);
        verifyRepeatDto.setVerifyKeyValue(tenantType.getName());
        if (!tenantTypeMapper.checkNameNotRepeat(verifyRepeatDto)) {
            throw new BizServiceException("租户类型名称不能重复！");
        }
        verifyRepeatDto.setVerifyKeyValue(tenantType.getCode());
        if (!tenantTypeMapper.checkCodeNotRepeat(verifyRepeatDto)) {
            throw new BizServiceException("租户类型编码不能重复！");
        }
        return Boolean.TRUE;
    }
}
